<?php
/*首页数据*/
namespace Admin\Model;
use Think\Model;
class MyinfoModel extends Model {
    public function isGetMyinfo($data = ""){
        $admin  = M('Admin');
        $Cars   = M('Cars');
        $City   = M('City');
        $order   = M('Order');
        $orderoperation   = M('order_operation');
        $data   = $admin->where(array('id'=>session('lampzuo_id')))->find();
        if(!$data){
        	return false;
        }
        $data['insert_time'] = date('Y-m-d H:i:s',$data['insert_time']);
        if($data['position'] == "1"){
        	$m['status']  = array('EQ',0);
            $m['usernum'] = array('EQ',$data['usernum']);
            $res =  $orderoperation->where($m)->field('orderid')->select();
            $count = count($res);
            for($i = 0; $i < $count; $i++){
                if($sum){
                    $sum.=",'".$res[$i]['orderid']."'";
                }else{
                    $sum.="'".$res[$i]['orderid']."'";
                }
            }
            $wh  ="1=1";
            $wh .=" and orderid in (".$sum.")";
            $success    = 0;//本月成功单
            $refuse     = 0;//本月被拒单
            $month      = 0;//本月单量
            $monthcount = 0;//总成功单量
            $countrefuse= 0;//总被拒单量
            $resadd = $order->where($wh)->select(); //总单量
            foreach ($resadd as $key => $value) {
            	$time1 = date('Y-m',time());
            	$time2 = date('Y-m',$value['create_time']);
            	if($time1==$time2){
            		$month += 1;
            		if($value['status'] == "-1"){
            			$refuse +=1;
            		}
            		if($value['status'] == "5"){
            			$success +=1;
            		}
            	}
            	if($value['status'] == '5'){
            		$monthcount += 1;
            	}
            	if($value['status'] == "-1"){
            			$countrefuse +=1;
            	}
            }
            $data['success']    = $success;
            $data['refuse']     = $refuse;
            $data['month']      = $month;
            $data['monthcount'] = $monthcount;
            $data['infocount']  = count($resadd);
            $data['monthcount']  = $monthcount;
            return $data;
        }elseif ($data['position'] == "3") {
        	$success    = 0;//本月成功单
            $refuse     = 0;//本月被拒单
            $month      = 0;//本月单量
            $monthcount = 0;//总成功单量
            $countrefuse= 0;//总被拒单
        	$m['cityid'] = array('EQ',$data['cityid']);
        	$resadd = $order->where($wh)->select(); //总单量
            foreach ($resadd as $key => $value) {
            	$time1 = date('Y-m',time());
            	$time2 = date('Y-m',$value['create_time']);
            	if($time1==$time2){
            		$month += 1;
            		if($value['status'] == "-1"){
            			$refuse +=1;
            		}
            		if($value['status'] == "5"){
            			$success +=1;
            		}
            	} 
            	if($value['status'] == "-1"){
            			$countrefuse +=1;
            	}
            	if($value['status'] == '5'){
            		$monthcount += 1;
            	}
            }
            $usernum = $admin->where(array('position'=>1))->count();
        	$data['success']    = $success;
            $data['refuse']     = $refuse;
            $data['month']      = $month;
            $data['monthcount'] = $monthcount;
            $data['infocount']  = count($resadd);
            $data['monthcount'] = $monthcount;
            $data['countrefuse'] = $countrefuse;
            $data['usercount']    = $usernum;
            return $data;
        }elseif ($data['position'] == "301") {
            $success    = 0;//本月成功单
            $refuse     = 0;//本月被拒单
            $month      = 0;//本月单量
            $monthcount = 0;//总成功单量
            $countrefuse= 0;//总被拒单
            $m['cityid'] = array('EQ',$data['cityid']);
            $resadd = $order->where($wh)->select(); //总单量
            foreach ($resadd as $key => $value) {
                $time1 = date('Y-m',time());
                $time2 = date('Y-m',$value['create_time']);
                if($time1==$time2){
                    $month += 1;
                    if($value['status'] == "-1"){
                        $refuse +=1;
                    }
                    if($value['status'] == "5"){
                        $success +=1;
                    }
                } 
                if($value['status'] == "-1"){
                        $countrefuse +=1;
                }
                if($value['status'] == '5'){
                    $monthcount += 1;
                }
            }
            $usernum = $admin->where(array('position'=>1))->count();
            $data['success']    = $success;
            $data['refuse']     = $refuse;
            $data['month']      = $month;
            $data['monthcount'] = $monthcount;
            $data['infocount']  = count($resadd);
            $data['monthcount'] = $monthcount;
            $data['countrefuse'] = $countrefuse;
            $data['usercount']    = $usernum;
            return $data;
        }elseif ($data['position'] == "4") {
        	$success    = 0;//本月成功单
            $refuse     = 0;//本月被拒单
            $month      = 0;//本月单量
            $monthcount = 0;//总成功单量
            $countrefuse= 0;//总被拒单
        	$m['provinceid'] = array('EQ',$data['provinceid']);
        	$resadd = $order->where($wh)->select(); //总单量
            foreach ($resadd as $key => $value) {
            	$time1 = date('Y-m',time());
            	$time2 = date('Y-m',$value['create_time']);
            	if($time1==$time2){
            		$month += 1;
            		if($value['status'] == "-1"){
            			$refuse +=1;
            		}
            		if($value['status'] == "5"){
            			$success +=1;
            		}
            	} 
            	if($value['status'] == "-1"){
            			$countrefuse +=1;
            	}
            	if($value['status'] == '5'){
            		$monthcount += 1;
            	}
            }
            $usernum   = $admin->where(array('position'=>1))->count();
            $usercsnum = $admin->where(array('position'=>3))->count();
        	$data['success']    = $success;
            $data['refuse']     = $refuse;
            $data['month']      = $month;
            $data['monthcount'] = $monthcount;
            $data['infocount']  = count($resadd);
            $data['monthcount'] = $monthcount;
            $data['countrefuse']= $countrefuse;
            $data['usercount']  = $usernum;
            $data['usercscount']= $usercsnum;
            return $data;
        }
    }
     /**
     * 获取车辆信息
     * @return [type] [description]
     */
    public function getVehicle($keys=false){
        $admin  = M('Admin');
        // $Cars   = M('Cars');
        $Cars   = M('qcgl_manage',null);
        $interestrate   = M('interest_manage',null);
        $City   = M('City');
        $stock  = M('stock');
        $price  = M('price');
        $Carscolour   = M('cars_stock');
        $data   = $admin->where(array('id'=>session('lampzuo_id')))->find();
        $result = $stock->where(array('is_show'=>1,'city_id'=>$data['stock_provinceid']))->select();
        if(!$result){return false;}
        $count1 = count($result);
        $city_arr=[];
        for($i = 0; $i < $count1; $i++){
            $sum1.=$sum1?",'".$result[$i]['id']."'":"'".$result[$i]['id']."'";
            $city_arr[$result[$i]['id']]=$result[$i];
        }
        $w  ="1=1";
        $w .=" and sid in (".$sum1.")";
        $resultadd = $Carscolour->where($w)->order('stock desc')->select();
        $color_arr=[];
        $stock_arr=[];
        foreach ($resultadd as $key => $value) {
            $stock_arr[$resultadd[$key]['carid']]["stock"] +=$value['stock'];
            $resultadd[$key]['name'] = $city_arr[$value['sid']]['city'];
            $color_arr[$resultadd[$key]['carid']][$value["id"]]=$resultadd[$key];
            $sum2.=$sum2?",'".$value['carid']."'":"'".$value['carid']."'";
        }
        $wl  ="1=1";
        $wl .=" and id in (".$sum2.")";
        // $wl .=" and is_show = 1";
        $Carsarr = $Cars->where($wl)->select();
        foreach ($Carsarr as $key => $value) {
            $arr1=$color_arr[$value['id']];
            $value['stockcount']  = $stock_arr[$value['carid']]['stock'];
            $arr2=[];
            $objarr=[];
            foreach($arr1 as $v){
                // $pricestock = $Cars->where(array('id'=>$v['carid']))->field('ishdcx,cszdj,xsjg')->find();
                $pricestock = $Cars->where(array('id'=>$v['carid']))->find();
                $rate = $interestrate->select();
                $v['price'] = $pricestock;
                $v['rate'] = $rate;
                // // $v['price'] = $price->where(array('city_id'=>$pricestock['city_id'],'carid'=>$v['carid']))->select();
                if(in_array($v['color'],$arr2)){
                   $k=array_search($v['color'],$arr2);
                   $objarr[$k]['city'][]=$v;
                }else{
                    $arr2[]=$v['color'];
                    $obj=[];
                    $obj['colorname']=$v['color'];
                    if($keys){
                        $obj['city'][$v['id']]=$v;
                    }else{
                        $obj['city'][]=$v;
                    }
                    $objarr[]=$obj;
                }
            }
            $value['colorarr']=$objarr;
            if($keys){
                $carcolor_arr[$value['id']]=$value;
            }else{
                $carcolor_arr[]=$value;
            }  
        }
        if(!$keys){
            array_multisort(array_column($carcolor_arr,'stockcount'),SORT_DESC,$carcolor_arr);
        }
        return $carcolor_arr;
    }

     /**
     * 获取所有车辆信息
     * @return [type] [description]
     */
    public function getAllVehicle($keys=false){
        $admin  = M('Admin');
        $cars   = M('qcgl_manage',null);
        $interestrate   = M('interest_manage',null);
        $brandDb=M('s_sjzd', null);
        $where = ['type' => '品牌','isdel'=>'0'];
        $brandData = $brandDb -> where($where) -> field('id, content as brand') -> select();
        $brands = [];
        foreach ($brandData as $k => $v) {
            if (!in_array(['brand' => $v['brand']], $brands)) {
                $brands[$v['id']] = ['brand' => $v['brand']];
            }
        }
        $data['brand']=$brands;
        $cars=M('qcgl_manage', null);
        $where="is_show=1";
        $carData=$cars ->field('id, cs, qcmc, cszdj, ppid,xsjg,yg,sf,ishdcx') -> select();
        foreach($carData as $val){
            $data['cars'][$val['ppid']][]=$val;
        }
        $rate = $interestrate->select();
        $datas = ['result'=>$data,'rate'=>$rate];
        return $datas;
    }
    /**
     * 返回指定个人资料
     * @param $id. 管理员ID
     * @param  $field  需要的字段
     */
    public function getinfofield($id,$field) {
        $channel_db = D('Channel');
        if($field){
            $field = implode(',', $field);
        }else{
            $field = true;
        }
        if($id){
            $id_key = array_keys($id);
            $id_value = array_values($id);
            $where = $id_key[0].'='.$id_value[0];
        }else{
            return false;
        }
        
        $admin_mdb = M('admin');
        $admin_data = $admin_mdb->where($where)->field($field)->find();
        if(!$admin_data){
             return false;
        }
        $channelmdb = M('wayuser');
        $res = $channelmdb->where(array('admin_id'=>$id['id'],"channeltype"=>2))->field('waynum')->find();
        if ($res) {
            $admin_data['waynum'] = $res['waynum'];
        }
        $channel_data = $channel_db->DataWhere(['admin_id'=>$admin_data['id']],['id','waynum']);
        $yuqi = 0;
        $order_success = 0;
        $beiju = 0;
        $zongdan = 0;
        $chenggonglv = 0;
        $benyue_beiju = 0;
        $benyue_danshu = 0;
        $benyue_cgl = 0;
        $chenggonglv = 0;
        $zongdan = $channel_db->ordercount_channel($admin_data['id'],'ordercount');
        $yuqi = $channel_db->ordercount_channel($admin_data['id'],'yuqi');
        $beiju = $channel_db->ordercount_channel($admin_data['id'],'allrefuse');
        $benyue_beiju = $channel_db->ordercount_channel($admin_data['id'],'month_refuse');
        $benyue_danshu = $channel_db->ordercount_channel($admin_data['id'],'month_ordercount');
        $order_success = $channel_db->ordercount_channel($admin_data['id'],'allsuccess');
        $benyue_order_success = $channel_db->ordercount_channel($admin_data['id'],'month_success');
        if($zongdan>0&&$order_success>0){
            $chenggonglv = round($order_success/$zongdan*100,0);
            $chenggonglv = $chenggonglv.'%';
        }else{
            $chenggonglv = '暂无成功订单';
        }
        if($benyue_danshu>0&&$benyue_order_success>0){
            $benyue_cgl = round($benyue_order_success/$benyue_danshu*100,0);
            $benyue_cgl = $benyue_cgl . '%';
        }else{
            $benyue_cgl = '本月暂无成功订单';
        }
        $admin_data['yuqi'] = $yuqi;
        $admin_data['successorder'] = $order_success;
        $admin_data['beiju'] = $beiju;
        $admin_data['zongdan'] = $zongdan;
        $admin_data['chenggonglv'] = $chenggonglv;
        $admin_data['benyue_beiju'] = $benyue_beiju;
        $admin_data['benyuechenggong'] = $benyue_order_success;
        $admin_data['benyue_danshu'] = $benyue_danshu;
        $admin_data['benyue_cgl'] = $benyue_cgl;
        $admin_data['insert_time'] = date('Y-m-d',$admin_data['insert_time']);
        if($admin_data['position']=='3'){
            $admin_data['gw_count'] = $this->SzcsShow($admin_data['id']);
        }else if($admin_data['position']=='4'){
            $gwsz = $this->SzcsShow($admin_data['id']);
            $admin_data['gw_count'] = $gwsz['sheng_gw'];
            $admin_data['sz_count'] = $gwsz['sheng_sz'];
        }if($admin_data['position']=='1100'){
            $gwsz = $this->SzcsShow($admin_data['id']);
            $admin_data['gw_count']      = $gwsz['sheng_gw'];
            $admin_data['sz_count']      = $gwsz['sheng_sz'];
            $admin_data['quanguo_count'] = $gwsz['sheng_quanguo'];
        }
        return $admin_data;
    }

    public function SzcsShow($id) {
       $admin_mdb = M('admin');
       $me_data = $admin_mdb->where('id='.$id)->find();
       $position = ['3','4','1100'];
       if(!in_array($me_data['position'],$position)){
        return false;
       }
       if($me_data['position']=='3'){
        $city_GW = $admin_mdb->where('position=1 and cityid='.$me_data['cityid'].' and pid='.$me_data['id'].$this->istest['testsql'])->count();
        return $city_GW;
       }
       if($me_data['position']=='4'){
        $province_GW = $admin_mdb->where('position=1 and provinceid='.$me_data['provinceid'].$this->istest['testsql'])->count();
        $province_SZ = $admin_mdb->where('position=3 and provinceid='.$me_data['provinceid'].$this->istest['testsql'])->count();
        return ['sheng_sz'=>$province_SZ,'sheng_gw'=>$province_GW];
       }
       if($me_data['position']=='1100'){
        $province_GW = $admin_mdb->where('position=1'.$this->istest['testsql'])->count();
        $province_SZ = $admin_mdb->where('position=3'.$this->istest['testsql'])->count();
        $quanguo     = $admin_mdb->where('position=4'.$this->istest['testsql'])->count();
        return ['sheng_sz'=>$province_SZ,'sheng_gw'=>$province_GW,'sheng_quanguo'=>$quanguo];
       }

    }

    
    public function OneFieldData($adminid,$field=true) {
        $admin_mdb = M('admin');
        if($field!==true){
            $field = implode(',', $field);
        }
        $me_data = $admin_mdb->where('id='.$adminid)->field($field)->find();
        return $me_data;
    }

     /**
     * 已付款订单
     */
    public function OrderAlreadyPay() {
        $order_mdb = M('order');
        $orderPay_mdb = M('order_pay');
        $orderOper_mdb = M('order_operation');
        $car_mdb       = M('cars');
        $user_mdb      = M('user');
        $count= $order_mdb->where('status=3')->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $order_data = $order_mdb->where('status=3')->limit($Page->firstRow.','.$Page->listRows)->select();
        for($i = 0; $i < count($order_data); $i++){
            if($sum){
                $sum.=",'".$order_data[$i]['orderid']."'";
            }else{
                $sum.="'".$order_data[$i]['orderid']."'";
            }
            if($sum1){
                $sum1.=",'".$order_data[$i]['uid']."'";
            }else{
                $sum1.="'".$order_data[$i]['uid']."'";
            }
            if($sum2){
                $sum2.=",'".$order_data[$i]['carnum']."'";
            }else{
                $sum2.="'".$order_data[$i]['carnum']."'";
            }
        }
        $m   ="1=1";
        $m  .=" and id in (".$sum1.")";
        $ma  ="1=1";
        $ma .=" and carnum in (".$sum2.")";
        $useradd = $user_mdb->where($m)->field('id,nickname,tel,waynum')->select();
        $carsadd = $car_mdb->where($ma)->field('name,carnum,guide_price,first_pay,type')->select();
        $mal  ="1=1";
        $mal .=" and orderid in (".$sum.")";
        $mal .=" and status = 3";
        $adminadd= $orderOper_mdb->where($mal)->select();
        return ['data'=>$order_data,'page'=>$Page];
    }



}

    
?>
